/****This program computes the predraft characteristics in Table 6
***/ 

***Install package: Distinct 
ssc inst distinct

*****Please replace here with the directory folder
cd "C:\Users\xinto\Dropbox\Vietnam Veteran Paper\Crime Paper\2020 JHR Data\7. Table 6\Population Level"

set more off
clear all
set memory 350m
set matsize 1000

program drop _all

global outcomes = "firstArrestBy18 probationBy18 incarcerationBefore1968 "

use pop_sample_03162020, clear 

do predraft_081216

gen phyOrSexAbuse=9
replace phyOrSexAbuse=1 if (abusedPhyBefore==1 | abusedSexBefore==1)
replace phyOrSexAbuse=0 if (abusedPhyBefore==0 & abusedSexBefore==0)
tab phyOrSexAbuse

keep weight Census birthYear white study id total_male_inmate /// 
birthMonth birthDay lott totalSample currentViolent nonviolent vv eligible /// 
contactJuvenile violent_desistence nonviolent_desistence violent_r_draft nonviolent_r_draft ///
violent_drug_draft nonviolent_drug_draft ygroup* birth monthPop drawingYear APNheight ///
seasonYear seasonPop seasonYear_group seasonYear_group* monthYear_group monthYear_group* ///
inmate completeHighSchool phyOrSexAbuse firstArrestBy18 probationBy18 incarcerationBefore1968 /// 


foreach outcome in $outcomes{

  foreach birthgroup in  1 {
  
  di "Computing predraft characteristics for `outcome' of birthgroup `birthgroup'"

   ***Step 1: Preserve the data 
   preserve 

   ****Step 2: Cohort sample is the indicator =1 if the inmate is in the respective birth cohort, 
   *with non-missing values for the draft-eligibility, veteran status and outcome variables 
   gen cohort_sample=. 

   *Note that the cohort_sample also mark out the VSUS records 
   replace cohort_sample=1 if ((inmate==0 & ygroup`birthgroup'==1) /// 
   |(totalSample==1 & ygroup`birthgroup'==1 & `outcome'!=9 & inmate==1 & Census==0))

   replace cohort_sample=0 if cohort_sample==. & (Census==0|inmate==0) 
   
   ****Step 3: Create birth_cohort variable to mark out the corresponding birth cohort
   gen birth_cohort=.
   replace birth_cohort=1 if ygroup`birthgroup'==1 & (inmate==0 | Census==0) 
   replace birth_cohort=0 if birth_cohort==. & (inmate==0 | Census==0) 
   
   ****Step 4: Drop the observation of the SIPP data that are not in the birth cohort  
   drop if Census==1 & (ygroup`birthgroup'!=1 | weight==.)
   
   ***Step 5: svy_male_inmate is the variable with the values of the total inmates incarcerated in state prison-
   * -in 1979, 1986, and 1991, and in the federal prison in 1991 respectively 
   gen svy_male_inmate=total_male_inmate if Census==0 

   gen T=vv
   gen Z=eligible
   ***Step 6: Y is the binary indicator to compute the proportion of inmates conditional on veteran status and draft-eligibilities-
   * within I2_pop_0524_be
   gen Y=`outcome'*cohort_sample
   replace Y=0 if Y==. & (Census==0|inmate==0)
   gen W=weight 

   ***Step 7: I2_pop_0524_be is the formula to compute the mean estimates and the bounds controling for the birth month and year effects  
   *predraft_052716
   di "input the proportion of the white and nonwhite sample"
   sca Cc=0.12825302
   sca li Cc 
   
   sca Cat=0.21017269
   sca li Cat 
   
   sca Cnt=0.66157429 
   sca li Cnt 
   
   **Probabilities 
   sca p10=Cat
   sca p01=Cnt
   sca p11=Cat+Cc
   sca p00=Cnt+Cc
   sca li p10
   sca li p01
   sca li p11 
   sca li p00 
   
   **Conditional means of veterans and eligible 
   sca PrT1=0.26735256
   sca li PrT1
   sca PrT0=1-0.26735256
   sca li PrT0
   
   sca PrZ1=0.44583643
   sca li PrZ1 
   sca PrZ0=1-0.44583643
   sca li PrZ0
   
   predraft_081216
   
   *Step 8: bootstrapping (uncomment to run the bootstrap)
   *bootstrap _b, r(1000) str(Census) saving(vc_`outcome'_ygroup`birthgroup'_predraft_be.dta, double replace) seed(1989):  predraft_081216
   display("program finished running")
   
restore  

      }
}


